<template>
  <a-modal
    v-model:visible="dialogVisible"
    width="80%"
    title="复制权限"
    @ok="handleOk"
    okText="选中提交"
  >
    <div class="systemRoleManage-container">
      <searchData
        :search-data="searchOptions"
        @search="handleSearch"
      />
      <a-radio-group
        v-model:value="value"
        name="radioGroup"
        style="width: 100%"
        @change="radioChange"
      >
        <list-table
          ref="table"
          :columns="columns"
          :request="loadData"
          :alert="true"
          row-key="id"
        >
          <template
            slot="checkedslot"
            slot-scope="text, record, index"
          >
            <a-radio :value="record.checked" />
          </template>
          <template
            slot="index"
            slot-scope="text, record, index"
          >
            <span>{{ index + 1 }}</span>
          </template>
          <template
            slot="enable"
            slot-scope="text"
          >
            {{ text ? '可用' : '不可用' }}
          </template>
        </list-table>
      </a-radio-group>
    </div>
  </a-modal>
</template>



<script>
  import { packageConfirm } from '@/utils/baseAction.js'
  import { getRoleManageListNew, deleteRoleList } from '@/api/system/system.js'
  const searchOptions = [
    {
      name: '名称',
      value: 'nameLike',
      type: 'input',
    },
    {
      name: '编码',
      value: 'codeLike',
      type: 'input',
    },
  ]

  const columns = [
    {
      title: '',
      dataIndex: 'checked',
      width: '65px',
      scopedSlots: { customRender: 'checkedslot' },
    },
    {
      title: '序号',
      dataIndex: 'index',
      width: '65px',
      scopedSlots: { customRender: 'index' },
    },
    {
      title: '名称',
      dataIndex: 'name',
    },
    {
      title: '编码',
      dataIndex: 'code',
    },
    {
      title: '业务标识',
      width: '100px',
      dataIndex: 'commandName',
    },
    {
      title: '绑定岗位',
      dataIndex: 'postAllName',
    },
    {
      title: '状态',
      dataIndex: 'enable',
      width: '100px',
      scopedSlots: { customRender: 'enable' },
    }
  ]
  export default {
    data() {
      return {
        value: '',
        dialogVisible: false,
        childInitData: {},
        visible: {
          addResource: false,
        },
        searchOptions,
        columns,
        queryParam: {},
        statisticsInfo: {
          totalNum: 0,
        },
        tableData: [],
        cacheData: {
          role: {id: ''}
        }
      }
    },
    methods: {
      radioChange(e) {
        const data = JSON.parse(JSON.stringify(this.tableData))
        this.cacheData = data[e.target.value]
      },
      handleOk() {
        if(!this.cacheData.role.id) {
          this.$message.warn("请选择要复制的权限")
          return
        }
        this.$emit("cppyFinish", this.cacheData.role.id)
        this.close()
      },
      open() {
        this.dialogVisible = true
      },
      close() {
        this.dialogVisible = false
      },
      handleAddSourceConfirm() {
        this.handleCancel()
        this.handleSearch()
      },
      handleCancel() {
        this.visible.addResource = false
      },
      async loadData(parameter) {
        // try {
        const requestParameters = Object.assign({}, { ...parameter, pageIndex: parameter.pageNo }, this.queryParam)
        return getRoleManageListNew(requestParameters).then(res => {
          res.code = res.status;
          res.totalSize = res.data.total;
          this.statisticsInfo.totalNum = res.data.total;
          const arr = []
          res.data.list.forEach((item, index)=>{
            const obj = {...item,...item.role,checked: index}
            arr.push(obj)
          })
          res.data = arr;
          this.tableData = arr;
          const idx = this.tableData.findIndex(item => item.role.id === this.cacheData.role.id)
          if(idx > -1) {
            this.value = idx;
          } else {
            this.value = ''
          }
          return res;
        })
      },
      handleSearch(val) {
        this.queryParam = {
          ...this.queryParam,
          ...val,
        }
        this.$refs.table.refresh(true)
      },
      handleUpdate(type, data = {}) {
        this.childInitData = { ...data }
        const commonHandler = () => {
          this.$router.push({
            path: '/systemManage/menuPermissions/systemRoleManage/updateRole',
            query: {
              pageType: type,
              id: data.id || '',
            },
          })
        }
        const map = {
          add: commonHandler,
          edit: commonHandler,
          look: commonHandler,
          delete: () => {
            this.deleteRoleListReq(data)
          },
          addSource: () => {
            this.visible.addResource = true
          },
        }
        map[type] && map[type]()
      },
      async deleteRoleListReq(data) {
        packageConfirm({
          callback: async () => {
            const res = await deleteRoleList(data)
            this.$message.success(res.message || res.data.message)
            this.handleSearch()
          }
        })
      },
    },
  }
</script>

<style scoped lang="less">
    /deep/ .ant-card {
        box-shadow: none!important;
    }
    /deep/ .ant-card-body {
        padding: 0 15px;
    }
    /deep/ .titleName {
        display: none;
    }
    /deep/ .ant-form-item-label {
        width: 52px;
    }
    /deep/ .anticon-setting.tableIcon {
        display: none;
    }
</style>
